/**
 * Created by liebling on 2017/7/9.
 */
import $script from 'scriptjs';
import '../main';
import '../../css/userCenter/userManage.less';
import '../../css/parts/common.less';
import '../../../public/plugins/xui/xui.1.0.0.css';

var ApiService = require('../module/ApiService');
var UserService = require('../module/UserService');

$script.ready('jqueryFile', function () {

	require('../component/common/header');
	require('../component/common/header2');
	require('../component/common/workNav');
	require('../component/common/list');
	require('../../../public/plugins/laydate/laydate');
	require('../../../public/plugins/jquery.validate');
	require('../../../public/plugins/jquery.validate.extend');
	let xui = require('../../../public/plugins/xui/xui.1.0.0');

	var index = layer.load(1, {shade: [1, '#fff'], scrollbar: false});
	let userGroupList = [];
	ApiService.postApi('userGroupList', {pageSize: 9999})
		.then(function (res) {
			userGroupList = res.data.results;
			xui.picker({
				'element': $('#userGroupName'),
				'value': _.map(userGroupList, function (item) {
					return {
						name: item.name,
						value: item._id,
					};
				}),
				'onClose': function (val, element, selectCol) {
					$('#userGroup').val(selectCol.data('value'));
				}

			});
			layer.close(index);
			return null;
		});
	//验证初始化
	;(function () {
		$.validator.addMethod('isPWOK', function (value, element) {
			var reg = /^[A-Za-z0-9]{6,16}$/;
			if (reg.test(value) || value == "") {
				return true;
			} else {
				return false;
			}
		}, '6-16位密码(字母或数字)');
		$.validator.addMethod('isPhoneOK', function (value, element) {
			var reg = /^(0\d{2,4})?-?\d{7,8}$/;
			if (reg.test(value) || value == "") {
				return true;
			} else {
				return false;
			}
		}, '座机号码非法');
		$.validator.addMethod('isEmailOK', function (value, element) {
			var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;
			if (reg.test(value) || value == "") {
				return true;
			} else {
				return false;
			}
		}, '邮箱非法');
		$.validator.addMethod('isMobileOk', function (value, element) {
			var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
			if (reg.test(value) || value == "") {
				return true;
			} else {
				return false;
			}
		}, '手机号非法');

		$('#createUser').validate({
			errorElement: "label",
			onkeyup: false,
			ignore: '.notCheck',
			errorPlacement: function (er, el) {
				$(el).removeClass("m-success-con");
				$(el).addClass("m-error-con");
				$(el).closest(".fn-item").find(".xui-wordError").append(er);
			},
			success: function (element, callblack) {
				$(element).remove();
				$(callblack).removeClass("m-error-con");
				$(callblack).addClass("m-success-con");
			},
			onfocusout: function (element) {
				$(element).valid();
			},
			rules: {
				project: {
					required: true
				},
				realName: {
					required: true
				},
				email: {
					required: true,
					isEmailOK: true
				},
				mobile: {
					required: true,
					number: true,
					minlength: 11,
					isMobileOk: true
				},
				phone: {
					isPhoneOK: true
				},
				password: {
					required: true
				},
				password_confirm: {
					required: true,
					equalTo: '#password'
				}
			},
			messages: {
				project: {
					required: '必填'
				},
				realName: {
					required: '必填'
				},
				email: {
					required: '必填'
				},
				mobile: {
					required: '必填',
					number: '数字',
					minlength: '11位',
				},
				phone: {
					isPhoneOK: '座机号码不正确',
				},
				password: {
					required: '必填'
				},
				password_confirm: {
					required: '必填',
					equalTo: '不相同'
				}
			},
			submitHandler: function (from) {
				var params = ApiService.serialize($(from).find('input[name],select[name]'));
				_.set(params, 'userName', params.realName);
				ApiService.postApi('createUser', params)
					.then(ApiService.error)
					.then(function (res) {
						layer.msg(res.message, {
							time: 2000
						}, function () {
							window.location.reload();
						})
					})
					.catch(function (err) {
					});
				return false;
			}
		});

		$('#isMobilePW').click(function () {
			var status = $(this).is(':checked');
			console.log('status', status)
			if (status) {
				//password
				// password_confirm
				$('#mobile').on('blur', function () {
					console.log('blur', $(this).val());
					$('#password,#password_confirm').val($(this).val());
				});
				$('#mobile').trigger('blur');
			} else {
				$('#mobile').off('blur');
				$('#password,#password_confirm').val('');
			}
		})
	}());

});
